import QtQuick 2.4
import "Database.js" as Database

ModePageForm {
    id: modePageF
    Component.onCompleted: {
        //Database.initialize();
        listModelFromDatabase();
    }

    ListModel {
        id: childListModel
    }


    function listModelFromDatabase() {

        modePageListModel.clear();
        //childListModel.clear();
        //modePageListModel.childListmm.clear();
        var rows = Database.getModelFromModelName();
        if (rows.length > 0) {
            var index = 0;
            while(index < rows.length) {
                var aiitem = rows.item(index);
                var xrows = Database.getModelFromModel(aiitem.ModelID);
                var itemComponent = Qt.createQmlObject('import QtQuick 2.4;
                                                        Item {
                                                            property alias childListModel: childListModel;
                                                            ListModel {
                                                                id: childListModel;
                                                            }
                                                        }',modePageF,"childmodel");
                if (xrows.length > 0) {
                    var xindex = 0;
                    while(xindex < xrows.length) {
                        var xaiitem = xrows.item(xindex);
                        var modelName = Database.getTaskModeName(xaiitem.Model)
                        itemComponent.childListModel.append({"modeNameTxt":modelName,
                                                            "dosageTxt":xaiitem.DetergentDose,
                                                            "tempTxt":xaiitem.Temperature,
                                                            "timeTxt":xaiitem.Time});

                        //childListModel.append({"modeNameTxt":modelName,
                          //                      "dosageTxt":xaiitem.DetergentDose,
                         //                       "tempTxt":xaiitem.Temperature,
                          //                       "timeTxt":xaiitem.Time});
                        xindex++;
                    }
                }
                console.log(itemComponent.childListModel);
                modePageListModel.append({"itemID":aiitem.ModelID,
                                        "modeNameT":aiitem.ModelName,
                                         "childModel":itemComponent.childListModel});
                index++;
            }
        }
    }

    confirmButton {
        onClicked: {
            if(isEmpty()) {
                return;
            }
            addRectange.visible = false;
            var modeID = Database.createModeName(modeNameTextField.text);
            var suq = 1;
            if(xdComboBox.currentIndex != 0) {
                var taskModeId = Database.getTaskModeID(xdComboBox.currentText);
                Database.createMode(modeID,suq,taskModeId,xdTimeTextField.text,"","",xdTempTextField.text);
                suq++;
            }
            if(pxComboBox.currentIndex != 0) {
                var taskModeId = Database.getTaskModeID(pxComboBox.currentText);
                Database.createMode(modeID,suq,taskModeId,pxTimeTextField.text,"清洗剂",pxClearComboBox.currentText,pxTempTextField.text);
                suq++;
            }
            if(xcomboBox.currentIndex != 0) {
                var taskModeId = Database.getTaskModeID(xcomboBox.currentText);
                Database.createMode(modeID,suq,taskModeId,xTimeTextField.text,"消毒剂",xdyComboBox.currentText,xTempTextField.text);
                suq++;
            }
            if(hgComboBox.currentIndex != 0) {
                var taskModeId = Database.getTaskModeID(hgComboBox.currentText);
                Database.createMode(modeID,suq,taskModeId,hgTimeTextField.text,"","",0);
            }
            listModelFromDatabase();
        }
    }

    function isEmpty() {
        var xd = xdComboBox.currentText;
        var px = pxComboBox.currentText;
        var x = xcomboBox.currentText;
        var hg = hgComboBox.currentText;
        if(xd=="无" && px=="无" && x=="无" && hg=="无") {
            return true;
        }

        if(xd!="无") {
            var modeName = modeNameTextField.text;
            if(modeName=="") {
                return true;
            }
            var xdTime = xdTimeTextField.text;
            if(xdTime=="") {
                return true;
            }
            var xdTemp = xdTempTextField.text;
            if(xdTemp=="") {
                return true;
            }
        }
        if(px!="无") {
            var pxTime = pxTimeTextField.text;
            if(pxTime=="") {
                return true;
            }
            var pxTemp = pxTempTextField.text;
            if(pxTemp=="") {
                return true;
            }
        }
        if(x!="无") {
            var xTime = xTimeTextField.text;
            if(xTime=="") {
                return true;
            }
            var xTemp = xTempTextField.text;
            if(xTemp=="") {
                return true;
            }
        }
        if(hg!="无") {
            var hgTime = hgTimeTextField.text;
            if(hgTime=="") {
                return true;
            }
        }

        return false;
    }

    function saveMode() {

    }
}
